package com.hlf.modules.merchant.table.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.merchant.table.entity.Table;
import com.hlf.modules.merchant.table.entity.TableType;
import com.hlf.modules.merchant.table.service.TableService;
import com.hlf.modules.merchant.table.service.TableTypeService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 餐桌控制类
 */
@RestController
@RequestMapping("table/")
public class TableController extends AbstractController {

    @Autowired
    private TableService tableService;
    @Autowired
    private TableTypeService tableTypeService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public ReturnResult list(@RequestParam Map<String, Object> params) {
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        String storeId = user.getCurrentStoreId();
        params.put("storeId", storeId);
        params.put("userId", userId);
        PageUtils pageUtils = tableService.queryListForPage(params);
        return ReturnResult.ok().put("page", pageUtils);
    }

    /**
     * 查找餐桌类型(分页)
     */
    @RequestMapping("/tableTypeListPage")
    public ReturnResult tableTypeListPage(@RequestParam Map<String, Object> params) {
        SysUserEntity user = getUser();
        String storeId = user.getCurrentStoreId();
        params.put("storeId", storeId);
        params.put("userId",user.getUserId());
        PageUtils pageUtils = tableTypeService.queryListForPage(params);
        return ReturnResult.ok().put("page", pageUtils);
    }


    /**
     * 通过id查找餐桌信息
     */
    @RequestMapping("/info/{tableId}")
    public ReturnResult config(@PathVariable("tableId") String tableId) {
        Assert.isNull(tableId, "餐桌id不能为空!");
        Table table = tableService.queryObjectForId(Long.parseLong(tableId));
        return ReturnResult.ok().put("table", table);
    }

    /**
     * 通过Id查找商品类型
     */
    @RequestMapping("/tableType/{id}")
    public ReturnResult tableType(@PathVariable("id") String id) {
        TableType tableType = tableTypeService.queryObjectForId(Long.parseLong(id));
        return ReturnResult.ok().put("tableType", tableType);
    }

    /**
     * 查找餐桌类型
     */
    @RequestMapping("/tableTypeList")
    public ReturnResult tableTypeList() {
        SysUserEntity user = getUser();
        List<TableType> tableTypes = tableTypeService.queryListForStoreId(Long.parseLong(user.getCurrentStoreId()));
        return ReturnResult.ok().put("tableTypes", tableTypes);
    }

    /**
     * 新增餐桌
     */
    @RequestMapping("/saveTable")
    public ReturnResult saveGoods(@RequestBody Table table) {
        Assert.isBlank(table.getTableNum(), "餐桌编号不能为空");
        Assert.isNull(table.getTableType(), "餐桌类型不能为空");
        SysUserEntity user = getUser();
        Long userId = user.getUserId();
        String name = tableTypeService.queryObjectForId(table.getTableType()).getName();

        String tableNum = table.getTableNum().toUpperCase();
        int j = tableService.queryCountForTableNumAndStoreId(tableNum, user.getCurrentStoreId());
        if (j > 0){
            return ReturnResult.error("该餐桌编号已存在!");
        }

        table.setId(IdWorkerFactory.nextId());
        table.setTableName(name);
        table.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        table.setInsertBy(userId);
        table.setTableNum(tableNum);
        int i = tableService.insertSelective(table);
        if (i == 0) {
            return ReturnResult.error("创建失败!");
        }

        return ReturnResult.ok();
    }

    /**
     * 删除餐桌
     */
    @RequestMapping("/delete")
    public ReturnResult saveGoods(HttpServletRequest request, @RequestBody List<String> tableIds) {
        Assert.isNull(tableIds, "餐桌id不能为空");
        tableService.deleteTable(request, tableIds);
        return ReturnResult.ok();
    }

    /**
     * 修改餐桌
     */
    @RequestMapping("/updateTable")
    public ReturnResult updateGoods(@RequestBody Table table) {
        Assert.isNull(table.getTableType(), "餐桌类型id不能为空");
        Assert.isNull(table.getId(), "餐桌id不能为空");
        Assert.isNull(table.getTableNum(), "餐桌编号不能为空");

        Byte tableStatus = tableService.queryObjectForId(table.getId()).getTableStatus();
        if (!Constants.TABLE_STATUS_FREE.equals(tableStatus)) {
            throw new BusinessException("餐桌正在使用,请在空闲时操作!");
        }
        Long userId = getUserId();
        String name = tableTypeService.queryObjectForId(table.getTableType()).getName();
        table.setTableName(name);
        table.setTableNum(table.getTableNum().toUpperCase());
        table.setLastUpdateBy(userId);

        int i = tableService.updateByPrimaryKeySelective(table);
        if (i == 0) {
            return ReturnResult.error("修改失败!");
        }
        return ReturnResult.ok();
    }

    /**
     * 创建餐桌类型
     */
    @SysLog("创建餐桌类型")
    @RequestMapping("saveTableType")
    public ReturnResult createTableType(@RequestBody TableType tableType) {
        Assert.isBlank(tableType.getName(), "餐桌类型名称不能为空");
        Assert.isNull(tableType.getMinNum(), "允许最少就餐人数不能为空");
        Assert.isNull(tableType.getMaxNum(), "允许最大就餐人数不能为空");

        SysUserEntity user = getUser();
        Integer minInt = tableType.getMinNum();
        Integer maxInt = tableType.getMaxNum();
        if (maxInt > 255) {
            return ReturnResult.error("最多可选择255!");
        } else if (minInt >= maxInt) {
            return ReturnResult.error("最少人数不能超过最大人数!");
        }

        tableType.setId(IdWorkerFactory.nextId());
        tableType.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        tableType.setInsertBy(user.getUserId());

        int i = tableTypeService.insertSelective(tableType);
        if (i == 0) {
            return ReturnResult.error("添加失败!");
        }

        return ReturnResult.ok();
    }


    /**
     * 修改餐桌类型
     */
    @SysLog("修改餐桌类型")
    @RequestMapping("updateType")
    public ReturnResult updateTableType(@RequestBody TableType tableType) {
        Assert.isBlank(tableType.getName(), "餐桌类型名称不能为空");
        Assert.isNull(tableType.getMinNum(), "允许最少就餐人数不能为空");
        Assert.isNull(tableType.getMaxNum(), "允许最大就餐人数不能为空");
        Assert.isNull(tableType.getId(), "餐桌类型id不能为空");

        Integer minInt = tableType.getMinNum();
        Integer maxInt = tableType.getMaxNum();
        if (maxInt > 255) {
            return ReturnResult.error("最多可选择255!");
        } else if (minInt >= maxInt) {
            return ReturnResult.error("最少人数不能超过最大人数!");
        }

        Long userId = getUserId();
        tableType.setLastUpdateBy(userId);
        tableTypeService.updateByPrimaryKeySelective(tableType);
        return ReturnResult.ok();
    }

    /**
     * 删除餐桌类型
     */
    @SysLog("修改餐桌类型")
    @RequestMapping("deleteType")
    public ReturnResult deleteTableType(HttpServletRequest request, @RequestBody List<String> tableIds) {
        Assert.isNull(tableIds, "餐桌类型id不能为空");
        for (String tableId : tableIds) {
            tableTypeService.deleteTableType(request, tableId);
        }
        return ReturnResult.ok();
    }

}
